package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.HostLineService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 卜加超
 * @Date: 2018/12/17 15:55
 * @Description:  匿名抢聊控制器
 *
 */
@RestController
@RequestMapping("/hostLine")
public class HostLineController {
    @Resource
    private HostLineService hostLineService;
    @Resource
    private OpenApiService openApiService;
    @Resource
    private AppKeyService appkeyService;
    /***
     * 原匿名抢聊-1.3改为免费抢聊
     * 获取在线主播
     * @return List<User>
     */
    @GetMapping("/list")
    public List<User> getHostLine(HttpServletRequest request){
        CommonHead com = VerifyUtil.verifyCommonHead(request);
        //是否正在审核
        OpenApiStatus ops = openApiService.getOpenApi(com);
        List<User> userList = new ArrayList<>();
        if (ops != null && ops.getForwardAudit() && ops.getBackAudit()) {
            String jsonStr = appkeyService.getAppKeyByKey("hostOnLineList").getValue();
            userList = JSONArray.parseArray(jsonStr, User.class);
            userList.forEach(i->i.setIsCallLine("离线"));
        }else{
            userList = hostLineService.getHostLine();
            userList.forEach(i->{
                if(i.getCallType()!=0){
                    i.setIsCallLine("免费");
                }else{
                    //i.getIsCall()==1 &&
                    if(i.getIsVideo()==2){
                        i.setIsCallLine(i.getChatPrice()+"金币/分钟");
                        // && i.getIsCall()==2
                    }else if(i.getIsVideo()==1){
                        i.setIsCallLine(i.getVideoPrice()+"金币/分钟");
                    }else{
                        i.setIsCallLine(i.getVideoPrice()+"金币/分钟");
                    }
                }
            });
        }
        return userList;
    }

    /**
     * huami1.3免费聊
     * @return userId
     */
    @GetMapping("/list/free/{userId:^[1-9][0-9]*$}")
    public User getHostLineByFree(@PathVariable("userId") long userId){
        return hostLineService.getHostLineByFree(userId);
    }
    /**
     * huami1.3免费聊
     * @return userId
     */
    @GetMapping("/list/freeIcon/{userId:^[1-9][0-9]*$}")
    public JSONArray getHostLineByFreeIcon(@PathVariable("userId") long userId){
        return hostLineService.getHostLineByFreeIcon(userId);
    }


}
